
using System;
using System.Collections.Generic;
using LibSars;
using System.Threading;
using System.Windows.Forms;

namespace SarsClient
{
	public interface MessageHandler {
		bool Handle(SarsPacket packet, ConnectionManager cmgr);
	}
	
	public class MessageHandlerROUTING : MessageHandler {

		// Handle packet. Returns true if packet was handled, false if
		// this message handler is not responsible for packets of this type.
		public bool Handle(SarsPacket packet, ConnectionManager cmgr){
			// No routing directive
			if (packet.RecipientId == 0x0000 || packet.RecipientId == cmgr.OwnId)
				return false;
			if (packet.RecipientId != cmgr.OwnId){
				cmgr.SendPacket(packet);
				Logging.Logger.Debug("Routing!");
				return true;
			}
			return false;
			
		}
		
		
		public MessageHandlerROUTING(){

		}
	}
	
	public class MessageHandlerDISCARDFROM : MessageHandler {

		// Handle packet. Returns true if packet was handled, false if
		// this message handler is not responsible for packets of this type.
		public bool Handle(SarsPacket packet, ConnectionManager cmgr){
			return (packet.SenderId == this.SenderId);
		}
		
		UInt16 SenderId;
		public MessageHandlerDISCARDFROM(UInt16 _SenderId){
			this.SenderId = _SenderId;
		}
	}
	
	public class MessageHandlerCLMERGE : MessageHandler {

		// Handle packet. Returns true if packet was handled, false if
		// this message handler is not responsible for packets of this type.
		public bool Handle(SarsPacket packet, ConnectionManager cmgr){
			
			if (packet.Message.GetMTypeInt() != SarsPacketMessageUHASCL.MTypeInt)
				return false;
			
			SarsPacketMessageUHASCL message = packet.Message as SarsPacketMessageUHASCL;
			if (message.PeerList != null)
			{
				foreach(SarsPeerItem item in message.PeerList){
					cmgr.AddPeer(new Peer(item));
				}
			}
			
			
			return false;
			
				
		}
		ConnectionManager cmgr;
		public MessageHandlerCLMERGE(ConnectionManager _cmgr){
			cmgr = _cmgr;
		}
	}
	
	
	
	public class MessageHandlerDEMO : MessageHandler {

		// Handle packet. Returns true if packet was handled, false if
		// this message handler is not responsible for packets of this type.
		public bool Handle(SarsPacket packet, ConnectionManager cmgr){
			
			if (packet.Message.GetMTypeInt() != SarsPacketMessageSAITHIS.MTypeInt)
				return false;
			Logging.Logger.Debug(((SarsPacketMessageSAITHIS)packet.Message).Text);
			if (DoGUIDisplay != null){ // if action for message displaying defined
				
				new MethodInvoker(delegate(){
				  DoGUIDisplay(packet.SenderId,DateTime.Now, 0,((SarsPacketMessageSAITHIS)packet.Message).Text);
				}).BeginInvoke(null,null); // Invoke asynchronously
				
			}
			//DoGUIDisplay.Invoke(packet.SenderId,DateTime.Now, 0,"IMINURCHATEVENT");
			
			return false;
			
				
		}
		DelDisplayMessage DoGUIDisplay;
		public MessageHandlerDEMO(DelDisplayMessage _DoGUIDisplay){
			DoGUIDisplay = _DoGUIDisplay;
		}
	}
}
